const myArray = [1, 2, [3, 4, 5, [6, [7]]], 8, [9]]
// const flatArray = myArray.reduce((preval, curval, curindex) => {
// 	console.log('preval=', preval);
// 	console.log('curval=', curval);
// 	return preval.concat(curval)
// }, []);
// console.log(flatArray); //[ 1, 2, 3, 4, 5, [ 6, [ 7 ] ], 8, 9 ]

const flatten = (array, deep = 1) => {
	if (deep <= 0) return array;
	return array.reduce((preval, curval) => {
		return preval.concat(Array.isArray(curval) ? flatten(curval, deep - 1) : curval)
	}, [])
}
// function flatten (array,deep=1) {
// return   array.reduce((acc,val) => acc.concat(Array.isArray(val)? flatten(val,deep-1):val),[])
// }
const flatArray = flatten(myArray, Infinity)
console.log(flatArray);
